package scau.erps.view.business.staff;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.Material;
import scau.erps.entity.Team;
import scau.erps.service.MaterialService;
import scau.erps.service.impl.MaterialServiceImpl;

/**
 * 原材料业务操作界面
 * @author FYJ
 *
 */
public class MaterialPanel extends BusinessPanelTemplate {
	private static final long serialVersionUID = 1L;
	private MaterialTabelModel dataModel;

	public MaterialPanel() {
		super();
		this.setComboItemsValue();
		this.operationBtn1.setText("入库");
		this.operationBtn2.setText("还款");
		this.operationBtn3.setText("更新");
		this.dataModel = getMaterialDataModel();
		this.table.setModel(dataModel);
		this.approveBtn.setVisible(false);
	}

	public MaterialTabelModel getMaterialDataModel() {
		Team team = Erps.getCurrentTeam();
		MaterialService MaterialService = Erps.getIocInstance().get(
				MaterialServiceImpl.class);
		List<Material> list = null;
		if (team == null) { // 工作人员登陆
			list = MaterialService.getAllMaterialOrder();
		} else {
			list = MaterialService.getMaterial(team.getId(), null, null);
		}
		dataModel = new MaterialTabelModel(list);
		return dataModel;
	}

	@Override
	public void setComboItemsValue() {
		String[] materialType = new String[] { "原材料类型", "M1", "M2", "M3", "M4" };
		String[] materialOrderStatus = new String[] { "订单状态", "已申请", "已入库",
				"已付款" };
		this.addItems(businessType, materialType);
		this.addItems(businessStatus, materialOrderStatus);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == searchBtn) {
			String team = null;
			if (teamCombo.getSelectedIndex() != 0) {
				team = (String) teamCombo.getSelectedItem();
			}
			String type = null;
			if (businessType.getSelectedIndex() != 0) {
				type = (String) businessType.getSelectedItem();
			}
			String status = null;
			if (businessStatus.getSelectedIndex() != 0) {
				status = (String) businessStatus.getSelectedItem();
			}
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
			if(team !=null){
				filters.add(RowFilter.regexFilter(team));
			}
			if (type != null) {
				filters.add(RowFilter.regexFilter(type));
			} 
			if (status != null) {
				filters.add(RowFilter.regexFilter(status));
			} 
			sorter.setRowFilter(RowFilter.andFilter(filters));			
			this.table.setRowSorter(sorter);
			this.table.setModel(dataModel);
			
		} else if (e.getSource() == operationBtn1) {
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int materialId = (int) this.table.getValueAt(row, 0);
				MaterialService materialService = Erps.getIocInstance().get(
						MaterialServiceImpl.class);
				Material material = materialService.getMaterialById(materialId);
				if ("已申请".equals(material.getStatus())) {
					material.setStatus("已入库");
					materialService.updateMaterial(material);
					String message = "已入库原材料类型" + material.getType()
							+ "审批通过，请入库" + material.getNumber() + "原材料";
					JOptionPane.showConfirmDialog(this, message, "提示",
							JOptionPane.CLOSED_OPTION);
					this.table.setModel(getMaterialDataModel());
				} else {
					JOptionPane.showConfirmDialog(this, "该原材料已入库", "提示",
							JOptionPane.CLOSED_OPTION);
				}
			}
		} else if (e.getSource() == operationBtn2) {// TODO只更改了状态 还没更新付款金额
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int materialId = (int) this.table.getValueAt(row, 0);
				MaterialService materialService2 = Erps.getIocInstance().get(
						MaterialServiceImpl.class);
				Material material = materialService2.getMaterialById(materialId);
				if ("已入库".equals(material.getStatus())) {
					material.setStatus("已付款");
					materialService2.updateMaterial(material);
					String message = "已入库原材料类型" + material.getType()
							+ "已付款" + material.getNumber() + "原材料";
					JOptionPane.showConfirmDialog(this, message, "提示",
							JOptionPane.CLOSED_OPTION);
					this.table.setModel(getMaterialDataModel());
				} else {
					JOptionPane.showConfirmDialog(this, "该原材料已付款", "提示",
							JOptionPane.CLOSED_OPTION);
				}
			}
		}
		else if(e.getSource() == operationBtn2){
			this.table.setModel(getMaterialDataModel());
		}

	}

}

class MaterialTabelModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private String[] title = { "序号", "小组", "材料类型", "金额", "申请时间", "到货时间",
			"付款时间", "状态" };
	private Object[][] data = null;

	public MaterialTabelModel(List<Material> list) {
		data = new Object[list.size()][8];
		Iterator<Material> iter = list.iterator();
		for (int i = 0; i < list.size(); i++) {
			Material material = iter.next();
			data[i][0] = material.getId();
			data[i][1] = "第"+material.getTeam_Id()+"组";
			data[i][2] = material.getType();
			data[i][3] = material.getNumber();
			data[i][4] = material.getBeginTime();
			data[i][5] = material.getGetTime();
			data[i][6] = material.getPayTime();
			data[i][7] = material.getStatus();
		}
	}

	@Override
	public int getRowCount() {
		return this.data.length; // 行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; // 列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex]; // 取表格单元格值
	}

	public String getColumnName(int columnIndex) {
		return title[columnIndex]; // 显示标题
	}
}
